/********************************************************************************
 * Copyright (c)2016-2020, ShenZhen WanJiaAn Technology CO.,LTD.
 * All Rights Reserved.
 *
 * File Name     : config_management.c
 * Author        : joki.zhu
 * Date          : 2019-10-14
 *
 * Record        :
 * 1.Date        : 2019-10-14
 *   Author      : joki.zhu
 *   Modification: Created file
 ********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <pthread.h>
#include <sys/types.h>
#include <fcntl.h>
#include "tag_generator.h"
#include "config_management.h"
#ifdef  __HuaweiLite__
#include "app_common.h"
#endif

typedef struct {
	char segmentFile[32];
	char segmentStartStr[32];
	char segmentEndStr[32];
	pthread_mutex_t mutex;
}CONFIG_SEGMENT_INFO_S;

static CONFIG_SEGMENT_INFO_S gs_configEnv;
static CONFIG_SEGMENT_INFO_S g_configBoard;

static int config_segment_init()
{
    static char bInit = 0;
    
    if (bInit == 0) 
    {
    	memset(&gs_configEnv, 0, sizeof(gs_configEnv));
    	snprintf(gs_configEnv.segmentFile, sizeof(gs_configEnv.segmentFile), "%s", ENV_SEGMENT_FILE_PATH);
    	snprintf(gs_configEnv.segmentStartStr, sizeof(gs_configEnv.segmentStartStr), "%s", ENV_SEGMENT_START_FLAG);
    	snprintf(gs_configEnv.segmentEndStr, sizeof(gs_configEnv.segmentEndStr), "%s", ENV_SEGMENT_END_FLAG);
    	pthread_mutex_init(&gs_configEnv.mutex, NULL);
    	
    	memset(&g_configBoard, 0, sizeof(g_configBoard));
    	snprintf(g_configBoard.segmentFile, sizeof(g_configBoard.segmentFile), "%s", BOARDINFO_SEGMENT_FILE_PATH);
    	snprintf(g_configBoard.segmentStartStr, sizeof(g_configBoard.segmentStartStr), "%s", BOARDINFO_SEGMENT_START_FLAG);
    	snprintf(g_configBoard.segmentEndStr, sizeof(g_configBoard.segmentEndStr), "%s", BOARDINFO_SEGMENT_END_FLAG);
    	pthread_mutex_init(&g_configBoard.mutex, NULL);

    	bInit = 1;
    }
    
	return 0;
}


static pthread_mutex_t mutex;

static void env_lock()
{
	static int lock_init = 0;
	if(lock_init == 0) {
		pthread_mutex_init(&mutex, NULL);
		lock_init = 1;
	}
	pthread_mutex_lock(&mutex);
}

static void env_unlock()
{
	pthread_mutex_unlock(&mutex);
}

/**
 * file data type:
 *	[token1]
 *	parameter1=value1
 *	parameter2=value2
 *	[token2]
 *	parameter1=value1
 *	parameter2=value2
 *
 * input:
 *	@filename:env name
 *	@token:env token
 *	@parameter:parameter in token
 *	@value:return value, parameter value
 *
 * output:
 *	value length(char *)
 */
int read_env(const char *src, const char *token, const char *parameter, char *value)
{
	int ret = 0;

	char token_buffer[100] = "";
	char *file_buffer = NULL;
	char *temp_buffer = NULL;
	char *temp_start_p = NULL;
	char *temp_end_p = NULL;
	char *parameter_p = NULL;
	
	file_buffer = (char *)src;
	if( file_buffer == NULL ) {
		printf("ERROR: file buffer malloc failed\n");
		ret = -1;
		goto read_out;
	}

	/* find the token in file_buffer and copy to temp_buffer*/
	snprintf(token_buffer, sizeof(token_buffer), "%s]", token);
	temp_start_p = strstr(file_buffer, token);
	if( temp_start_p == NULL ) {
		printf("ERROR: serch the %s token failed\n", token);
		ret = -1;
		goto file_read_out;
	}

	temp_end_p = strchr(temp_start_p, '[');
	if( temp_end_p == NULL ) {
		temp_buffer = (char *)malloc(strlen(temp_start_p)+1);
		if( temp_buffer == NULL ) {
			printf("ERROR: temp_buffer malloc failed %s, %d\n", __func__, __LINE__);
			ret = -1;
			goto file_read_out;
		}
		memset(temp_buffer, 0x00, strlen(temp_start_p)+1);
		strcpy(temp_buffer, temp_start_p);
	} else {
		temp_buffer = (char *)malloc(temp_end_p-temp_start_p+1);
		if( temp_buffer == NULL ) {
			printf("ERROR: temp_buffer malloc failed %s, %d\n", __func__, __LINE__);
			ret = -1;
			goto file_read_out;
		}
		memset(temp_buffer, 0x00, temp_end_p-temp_start_p+1);
		strncpy(temp_buffer, temp_start_p, temp_end_p-temp_start_p);
	}

	/* find the parameter in temp_buffer */
	while(1) {
		/* if find but don't parameter we need, continue search */
		if (parameter_p != NULL)
			parameter_p = strstr(parameter_p+strlen(parameter), parameter);
		else /* first serch */
			parameter_p = strstr(temp_buffer, parameter);

		/* not find */
		if( parameter_p == NULL ) {
			//printf("ERROR: no %s this parameter in %s file\n", parameter, filename);
			ret = -1;
			goto temp_read_out;
		}

		/* find but don't we need */
		if( *(parameter_p + strlen(parameter)) != '=' )
			continue;
		else /* already find */
			break;
	}

	/* get the parameter value */
	parameter_p = strchr(parameter_p, '=');
	if( parameter_p == NULL ) {
		printf("ERROR: %s parameter no value\n", parameter);
		ret = -1;
		goto temp_read_out;
	}
	strtok(parameter_p, ";");

	/* copy the parameter value to return (char *value) */
	strcpy(value, parameter_p+1);
	//printf("%s = %s\n", parameter, value);
	ret = strlen(value);
temp_read_out:
	free(temp_buffer);
file_read_out:
read_out:
	return ret;
}

/**
 *           temp_start_p                       temp_end_p
 *                |                                  |
 *  -------------------------------------------------------------- file_buffer
 *                |   parameter_p    parameter_end_p |
 *                |       |                  |       |
 *                ------------------------------------             temp_buffer
 *                        |------------------|					   parameter_buffer
 */
int write_env(char *dst, int dst_size, char *token, char *parameter, char *value)
{
	int ret = 0;
	
	char token_buffer[100] = "";
	char file_buffer[1024] = "";
	char *temp_buffer = NULL;
	char *temp_start_p = NULL;
	char *temp_end_p = NULL;
	char *parameter_p = NULL;
	char *parameter_end_p = NULL;
	char *parameter_buffer = (char *)malloc(256);

	env_lock();
	/* check parameter_buffer malloc is successful and memset */
	if( parameter_buffer == NULL ) {
		printf("malloc parameter_buffer error \n");
		ret = -1;
		goto write_out;
	}
	memset(parameter_buffer, 0x00, 256);

	strcpy(file_buffer, dst);

	/* find the token in file_buffer, if don't find, create it */
	snprintf(token_buffer, sizeof(token_buffer), "%s]", token);
	if( strlen(file_buffer) < strlen(token_buffer) )
		temp_start_p = NULL;
	else
		temp_start_p = strstr(file_buffer, token_buffer);
	if( temp_start_p == NULL ) {
create_new:
		sprintf(parameter_buffer, "[%s];%s=%s;", token, parameter, value);
		strcat(dst, parameter_buffer);
		goto file_write_out;
	}

	/* if token already created, copy token data to temp buffer */
	temp_end_p = strchr(temp_start_p, '[');
	if( temp_end_p == NULL ) {
		temp_end_p = file_buffer + strlen(file_buffer);
		temp_buffer = (char *)malloc(strlen(temp_start_p)+1);
		if( temp_buffer == NULL ) {
			printf("ERROR: temp_buffer malloc failed %s, %d\n", __func__, __LINE__);
			ret = -1;
			goto file_write_out;
		}
		memset(temp_buffer, 0x00, strlen(temp_start_p)+1);
		strcpy(temp_buffer, temp_start_p);
	} else {
		temp_buffer = (char *)malloc(temp_end_p-temp_start_p+1);
		if( temp_buffer == NULL ) {
			printf("ERROR: temp_buffer malloc failed %s, %d\n", __func__, __LINE__);
			ret = -1;
			goto file_write_out;
		}
		memset(temp_buffer, 0x00, temp_end_p-temp_start_p+1);
		strncpy(temp_buffer, temp_start_p, temp_end_p-temp_start_p);
	}

	/* find the parameter in temp_buffer */
	while(1) {
		/* if find but don't parameter we need, continue search */
		if (parameter_p != NULL)
			parameter_p = strstr(parameter_p+strlen(parameter), parameter);
		else /* first serch */
			parameter_p = strstr(temp_buffer, parameter);
		/* already find */
		if( (parameter_p != NULL) && (*(parameter_p+strlen(parameter)) == '=')) {
			parameter_end_p = strstr(parameter_p, ";");
			if( parameter_end_p == NULL ) {
				printf("parameter_end_p is NULL\n");
			}
			memset(dst, 0x0, dst_size);
			strncpy(dst, file_buffer, (temp_start_p+(parameter_p-temp_buffer)-file_buffer));
			sprintf(parameter_buffer, "%s=%s", parameter, value);
			strcat(dst, parameter_buffer);
			strcat(dst, (char *)(temp_start_p+(parameter_end_p-temp_buffer)));
			break;
		} else if(parameter_p != NULL){ /* find but not we need */
			continue;
		}else { /* not find */
			memset(dst, 0x0, dst_size);
			strncpy(dst, file_buffer, temp_end_p-file_buffer);
			sprintf(parameter_buffer, "%s=%s;", parameter, value);
			strcat(dst, parameter_buffer);
			strcat(dst, temp_end_p);
			break;
		}
	}

temp_write_out:
	free(temp_buffer);
file_write_out:
parameter_write_out:
	free(parameter_buffer);
write_out:

	env_unlock();
	return ret;
}

#define CMD_LINE "cmdline"
int cmdline_get_env(CONFIG_SEGMENT_INFO_S *pstSegmentInfo, char *token, char *param, char *result)
{
	char buf[1024] = "";
	int ret = 0;
	char *p = NULL;

	FILE *fb = fopen(pstSegmentInfo->segmentFile, "r");
	if(fb == NULL) {
		printf( "%s open file (%s) error\n", __func__, pstSegmentInfo->segmentFile);
		return -1;
	}

	ret = fread(buf, 1, sizeof(buf), fb);
	if(ret <= 0) {
		printf( "%s fread (%s) error\n", __func__, pstSegmentInfo->segmentFile);
		fclose(fb);
		return -1;
	}

	fclose(fb);

	p = strstr(buf, pstSegmentInfo->segmentStartStr);
	if(p == NULL) {
		printf( "%s fread (%s) error\n", __func__, pstSegmentInfo->segmentFile);
		return -1;
	}

    pthread_mutex_lock(&pstSegmentInfo->mutex);
    ret = read_env(p+5, token, param, result);
    pthread_mutex_unlock(&pstSegmentInfo->mutex);
    
	return ret;
}

int cmdline_set_env(CONFIG_SEGMENT_INFO_S *pstSegmentInfo, char *token, char *param, char *value)
{
	char cmdline[1024] = "";
	char buf[1024] = "";
	char env[1024] = "";
	char *ps = NULL, *pe = NULL;
	int ret = 0;

	FILE *fb = fopen(pstSegmentInfo->segmentFile, "r");
	if(fb == NULL) {
		printf( "%s open file (%s) error\n", __func__, pstSegmentInfo->segmentFile);
		return -1;
	}

	ret = fread(cmdline, 1, sizeof(cmdline), fb);
	if(ret <= 0) {
		printf( "%s fread (%s) error\n", __func__, pstSegmentInfo->segmentFile);
		fclose(fb);
		return -1;
	}

	fclose(fb);

	//printf("%s: cmdLine: %s\n", __func__, cmdline);

	ps = strstr(cmdline, pstSegmentInfo->segmentStartStr);
	if(ps == NULL) {
		printf( "%s fread (%s) error\n", __func__, pstSegmentInfo->segmentFile);
		return -1;
	}

	pe = strstr(cmdline, pstSegmentInfo->segmentEndStr);
	if(pe == NULL) {
		printf( "%s fread (%s) error\n", __func__, pstSegmentInfo->segmentFile);
		return -1;
	}
    
	strncpy(buf, ps+5, (pe-(ps+5)));
	//printf("%s: buf: %s, token:%s, param:%s value:%s\n", __func__, buf, token, param, value);
	pthread_mutex_lock(&pstSegmentInfo->mutex);
	write_env(buf, sizeof(buf), token, param, value);
    pthread_mutex_unlock(&pstSegmentInfo->mutex);
    
	//sprintf(env, "tag_generator --update --env=\"senv;%seenv;\" --tag_path=/dev/mtdblock1", buf);
	snprintf(env, sizeof(env), "%s%s%s;", pstSegmentInfo->segmentStartStr, buf, pstSegmentInfo->segmentEndStr);
	/*system(env);*/

	//printf("%s: %s\n", __func__, buf);

	fb = fopen(pstSegmentInfo->segmentFile, "w+");
	if(fb == NULL) {
		printf( "%s open file (%s) error\n", __func__, pstSegmentInfo->segmentFile);
		return -1;
	}

	ret = fwrite(env, 1, strlen(env), fb);
	if(ret <= 0) {
		printf( "%s fread (%s) error\n", __func__, pstSegmentInfo->segmentFile);
		fclose(fb);
		return -1;
	}

	fclose(fb);

	return 0;
}

static int buffer_to_file(char *fileName, char *inBufData, int inBufLen)
{
    FILE *fp = NULL;

    fp = fopen(fileName, "w+");
    if (fp == NULL) {
        printf("%s: open file %s failed\n", __FUNCTION__, fileName);
        return -1;
    }
    
    fwrite(inBufData, 1, inBufLen, fp);

    fflush(fp);
    fclose(fp);
    
    return 0;
}

static int file_to_buffer(char *fileName, char *outBufData, int maxLen)
{
    FILE *fp = NULL;

    fp = fopen(fileName, "r");
    if (fp == NULL) {
        printf("%s: open file %s failed\n", __FUNCTION__, fileName);
        return -1;
    }

    fread(outBufData, 1, maxLen, fp);

    fclose(fp);
    
    return 0;
}

int ConfigInit(void)
{
    int ret = 0;
    static char bInit = 0;
    
    if (bInit == 0)
    {
        char envValue[1024] = "";
        char boardInfoValue[1024] = ""; 
        
#ifdef __HuaweiLite__
        HI_mkdir(CONFIG_DIR_PATH, 0777);
#endif /* __HuaweiLite__ */

        /* Create default if detection does not exist */
        if (access(TAG_FILE_PATH, F_OK) != 0) {
             TagCreate(TAG_FILE_PATH, TAG_ENV_DEFAULT_VALUE, TAG_BOARDINFO_DEFAULT_VALUE, TAG_USER0_DEFAULT_VALUE);
#ifdef __HuaweiLite__
            LOGI("Create deault %s\n", TAG_FILE_PATH);
#endif /* __HuaweiLite__ */
        }
        
       
        TagGetValue(TAG_FILE_PATH, TAG_SEGMENT_TYPE_ENV, envValue, sizeof(envValue));
        TagGetValue(TAG_FILE_PATH, TAG_SEGMENT_TYPE_BOARDINFO, boardInfoValue, sizeof(boardInfoValue));
         
        buffer_to_file(ENV_SEGMENT_FILE_PATH, envValue, sizeof(envValue));
        buffer_to_file(BOARDINFO_SEGMENT_FILE_PATH, boardInfoValue, sizeof(boardInfoValue));
        
        config_segment_init();

#ifdef DEV_CONFIG
        ret = DevConfigInit();
        if (ret < 0) {
            LOGE("DevConfigInit failed\n");
            return -1;
        }
#endif /* DEV_CONFIG */
        
        bInit = 1;
    }

    return 0;
}

int ConfigSave(void)
{
    char envValue[1024] = "";
    char boardInfoValue[1024] = "";
    
    file_to_buffer(ENV_SEGMENT_FILE_PATH, envValue, sizeof(envValue));;
    file_to_buffer(BOARDINFO_SEGMENT_FILE_PATH, boardInfoValue, sizeof(boardInfoValue));;

    TagUpdate(TAG_FILE_PATH, envValue, boardInfoValue, NULL);

    return 0;
}

int ConfigGetEnv(char *token, char *param, char *result)
{
    return cmdline_get_env(&gs_configEnv, token, param, result);
}

int ConfigSetEnv(char *token, char *param, char *value)
{
    return cmdline_set_env(&gs_configEnv, token, param, value);
}

int ConfigGetBoardInfo(char *token, char *param, char *result)
{
     return cmdline_get_env(&g_configBoard, token, param, result);
}

int ConfigSetBoardInfo(char *token, char *param, char *value)
{
    return cmdline_set_env(&g_configBoard, token, param, value);
}

static void tagset_usage(void)
{
    printf("Usage: \n");
    printf("    tagset tag [create/update] [env_value] [boardinfo_value]\n");
    printf("    tagset tag [default/recovery/dump]\n");
    printf("    tagset wifi [ssid] [password] [open/wpa+wpa2/wep]\n");
    printf("    tagset log [value]\n");
    printf("    tagset uuid [value]\n");
    printf("    tagset alexaid [value]\n");	
    printf("    tagset version [value]\n");
    printf("    tagset packtime [value]\n");
    printf("    tagset env [token] [key] [value]\n");
    printf("    tagset board [token] [key] [value]\n"); 
}

int cmd_tagset(int argc, const char *argv[])
{
	if (argc == 0) {
        tagset_usage();
		return -1;
	}

#ifndef TAGSET_HOST
    ConfigInit();
#endif /* TAGSET_HOST */

    if (strcmp("tag", argv[0]) == 0)
    {
        if (argc == 1) {
            tagset_usage();
    		return -1;
        }
        
	    if (strcmp("create", argv[1]) == 0) 
	    {
	        if (argc == 4) {
                TagCreate(TAG_FILE_PATH, (char *)argv[2], (char *)argv[3], (char *)argv[4]);
	        } else {
                tagset_usage();
                return -1;
	        }
	    } 
	    else if (strcmp("update", argv[1]) == 0) 
	    {
	        if (argc == 4) {
                 TagUpdate(TAG_FILE_PATH, (char *)argv[2], (char *)argv[3], (char *)argv[4]);
	        } else {
                tagset_usage();
                return -1;
	        }
	    }
	    else if (strcmp("default", argv[1]) == 0)
	    {
	        TagCreate(TAG_FILE_PATH, TAG_ENV_DEFAULT_VALUE, TAG_BOARDINFO_DEFAULT_VALUE, TAG_USER0_DEFAULT_VALUE);
	    } 
	    else if (strcmp("recovery", argv[1]) == 0)
	    {
	        TagRecovery(TAG_FILE_PATH);
	    } 
	    else if (strcmp("dump", argv[1]) == 0) 
	    {
	        TagDump(TAG_FILE_PATH);
	    }
	    else 
	    {
            tagset_usage();
            return -1;
	    }
    }    
    else if (strcmp("wifi", argv[0]) == 0)
    {
        if (argc == 1) {
            char ssid[32] = "";
            char password[32] = "";
            char encrypt[32] = "";        
            ConfigGetEnv(ENV_WIFI_TOKEN, ENV_WIFI_KEY_SSID, ssid);
            ConfigGetEnv(ENV_WIFI_TOKEN, ENV_WIFI_KEY_PASSWORD, password);
            ConfigGetEnv(ENV_WIFI_TOKEN, ENV_WIFI_ENCRYPT, encrypt);
            printf("ssid: %s password: %s encrypt: %s\n", ssid, password, encrypt);
        } else if (argc == 4) {
             ConfigSetEnv(ENV_WIFI_TOKEN, ENV_WIFI_KEY_SSID, (char *)argv[1]);
             ConfigSetEnv(ENV_WIFI_TOKEN, ENV_WIFI_KEY_PASSWORD, (char *)argv[2]);
             ConfigSetEnv(ENV_WIFI_TOKEN, ENV_WIFI_ENCRYPT, (char *)argv[3]);
             ConfigSave();
        } else {
            tagset_usage();
            return -1;
        }
    }
    else if (strcmp("log", argv[0]) == 0)
    {
        if (argc == 1) {
            char loglevel[32] = "";
            ConfigGetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_LOGLEVEL, loglevel);
            printf("loglevel: %s\n", loglevel);
        } else if (argc == 2) {
             ConfigSetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_LOGLEVEL, (char *)argv[1]);
             ConfigSave();
        } else {
            tagset_usage();
            return -1;
        }
    } 
    else if (strcmp("uuid", argv[0]) == 0)
    {
        if (argc == 1) {
            char uuid[32] = "";
            ConfigGetEnv(ENV_FACTORY_TOKEN, ENV_FACTORY_KEY_UUID, uuid);
            printf("uuid: %s\n", uuid);
        } else if (argc == 2) {
             ConfigSetEnv(ENV_FACTORY_TOKEN, ENV_FACTORY_KEY_UUID, (char *)argv[1]);
             ConfigSave();
        } else {
            tagset_usage();
            return -1;
        }
    }   
	else if (strcmp("alexaid", argv[0]) == 0)
    {
        if (argc == 1) {
            char alexaid[32] = "";
            ConfigGetEnv(ENV_FACTORY_TOKEN, ENV_FACTORY_KEY_ALEXAID, alexaid);
            printf("alexaid: %s\n", alexaid);
        } else if (argc == 2) {
             ConfigSetEnv(ENV_FACTORY_TOKEN, ENV_FACTORY_KEY_ALEXAID, (char *)argv[1]);
             ConfigSave();
        } else {
            tagset_usage();
            return -1;
        }
    }
	#if 0
	else if (strcmp("pristat", argv[0]) == 0)
    {
        if (argc == 1) {
            char pristat[8] = "";
            ConfigGetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_PIRSTAT, pristat);
            printf("pristat: %s\n", pristat);
        } else if (argc == 2) {
             ConfigSetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_PIRSTAT, (char *)argv[1]);
             ConfigSave();
        } else {
            tagset_usage();
            return -1;
        }
    }
	else if (strcmp("timezone", argv[0]) == 0)
    {
        if (argc == 1) {
            char timezone[128] = "";
            ConfigGetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_TIMEZONE, timezone);
            printf("timezone: %s\n", timezone);
        } else if (argc == 2) {
             ConfigSetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_TIMEZONE, (char *)argv[1]);
             ConfigSave();
        } else {
            tagset_usage();
            return -1;
        }
    }	
	#endif
    else if (strcmp("version", argv[0]) == 0)
    {
        if (argc == 1) {
            char version[32] = "";       
            ConfigGetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_VERSION, version);
            printf("version: %s\n", version);
        } else if (argc == 2) {
             ConfigSetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_VERSION, (char *)argv[1]);
             ConfigSave();
        } else {
            tagset_usage();
            return -1;
        }
    }
    else if (strcmp("packtime", argv[0]) == 0)
    {
        if (argc == 1) {
            char packtime[32] = "";       
            ConfigGetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_PACKTIME, packtime);
            printf("packtime: %s\n", packtime);
        } else if (argc == 2) {
             ConfigSetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_PACKTIME, (char *)argv[1]);
             ConfigSave();
        } else {
            tagset_usage();
            return -1;
        }
    }    
    else if (strcmp("env", argv[0]) == 0)
    {
        if (argc == 3) {
            char result[32] = "";       
            ConfigGetEnv((char *)argv[1], (char *)argv[2], result);
            printf("%s: %s\n", argv[2], result);
        } else if (argc == 4) {
             ConfigSetEnv((char *)argv[1], (char *)argv[2], (char *)argv[3]);
             ConfigSave();
        } else {
            tagset_usage();
            return -1;
        }
    }
    else if (strcmp("board", argv[0]) == 0)
    {
        if (argc == 3) {
            char result[32] = "";       
            ConfigGetBoardInfo((char *)argv[1], (char *)argv[2], result);
            printf("%s: %s\n", argv[2], result);
        } else if (argc == 4) {
             ConfigSetBoardInfo((char *)argv[1], (char *)argv[2], (char *)argv[3]);
             ConfigSave();
        } else {
            tagset_usage();
            return -1;
        }
    }  
    else
    {
        tagset_usage();
        return -1;
    }
    
	return 0;
}

#ifdef TAGSET_HOST
int main(int argc, const char *argv[])
{
    cmd_tagset(--argc, ++argv);

    return 0;
}
#endif /* TAGSET_HOST */

